package com.shine.minepressure.ui.realTime;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.shine.minepressure.BR;
import com.shine.minepressure.R;
import com.shine.minepressure.app.Constants;
import com.shine.minepressure.base.BaseActivity;
import com.shine.minepressure.bean.AreaMessageBean;
import com.shine.minepressure.bean.SensorMessageBean;
import com.shine.minepressure.databinding.ActivitySearchBinding;
import com.shine.minepressure.http.ApiException;
import com.shine.minepressure.http.ECObserver;
import com.shine.minepressure.http.RetrofitClient;
import com.shine.minepressure.http.RxUtils;
import com.shine.minepressure.http.Service;
import com.shine.minepressure.ui.adapter.ViewPageAdapter;
import com.shine.minepressure.ui.realTime.viewModel.RealTimeDataViewModel;

import java.util.ArrayList;
import java.util.List;

public class RealTimeDataActivity extends BaseActivity<ActivitySearchBinding, RealTimeDataViewModel> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_search;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public RealTimeDataViewModel initViewModel() {
        return new RealTimeDataViewModel(this);
    }

    @Override
    public int appTheme() {
        return Constants.THEME_FULL;
    }

    @Override
    public void initData() {
        super.initData();
        setStatusBar();

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new RealTimeDataChild1Fragment());
        fragmentList.add(new RealTimeDataChild2Fragment());
        fragmentList.add(new RealTimeDataChild3Fragment());
//        fragmentList.add(new RealTimeDataChild4Fragment());
//        fragmentList.add(new RealTimeDataChild5Fragment());
//        fragmentList.add(new RealTimeDataChild6Fragment());

        ViewPageAdapter viewPageAdapter = new ViewPageAdapter(this, fragmentList);
        binding.viewPage2.setAdapter(viewPageAdapter);

        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(binding.tabLayout, binding.viewPage2, true, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                switch (position) {
                    case 0:
                        tab.setText("实时数据");
                        break;
                    case 1:
                        tab.setText("实时报警");
                        break;
                    case 2:
                        tab.setText("实时故障");
                        break;
//                    case 3:
//                        tab.setText("分站信息");
//                        break;
//                    case 4:
//                        tab.setText("测点信息");
//                        break;
//                    case 5:
//                        tab.setText("开出口");
//                        break;
                }
            }
        });
        tabLayoutMediator.attach();

        binding.back.setOnClickListener(this);
        binding.search.setOnClickListener(this);

//        getAreaMessage();

        getSensorMessage();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                default:
                    break;
            }
        }
    }


    private void getAreaMessage() {
        showLoading("");
        RetrofitClient.getInstance().create(Service.class)
                .getAreaMessage()
                .compose(RxUtils.bindToLifecycle(this))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .subscribe(new ECObserver<AreaMessageBean>() {
                    @Override
                    protected void _onNext(AreaMessageBean areaMessageBean) {
                        dismissLoading();

                        if (areaMessageBean.getTotal().equals("0"))
                            return;
                        List<String> areaMessage = new ArrayList<>();
                        for (AreaMessageBean.RowsDTO rowsDTO : areaMessageBean.getRows()) {
                            areaMessage.add(rowsDTO.getArea_Name());
                        }
                        RealTimeDataChild1Fragment.setAreaMessage(areaMessageBean.getRows(), areaMessage);
                        RealTimeDataChild2Fragment.setAreaMessage(areaMessageBean.getRows(), areaMessage);
                        RealTimeDataChild3Fragment.setAreaMessage(areaMessageBean.getRows(), areaMessage);
                        RealTimeDataChild4Fragment.setAreaMessage(areaMessageBean.getRows(), areaMessage);
                    }

                    @Override
                    protected void _onError(ApiException ex) {
                        ToastMessage(ex.message);
                        dismissLoading();
                    }
                });
    }


    private void getSensorMessage() {
        RetrofitClient.getInstance().create(Service.class)
                .getSensorMessage()
                .compose(RxUtils.bindToLifecycle(this))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .subscribe(new ECObserver<SensorMessageBean>() {
                    @Override
                    protected void _onNext(SensorMessageBean sensorMessageBean) {
                        dismissLoading();

                        if (sensorMessageBean.getTotal().equals("0"))
                            return;
                        List<String> sensorMessage = new ArrayList<>();
                        for (SensorMessageBean.RowsDTO rowsDTO : sensorMessageBean.getRows()) {
                            sensorMessage.add(rowsDTO.getSensorName());
                        }
                        RealTimeDataChild1Fragment.setSensorMessage(sensorMessageBean.getRows(), sensorMessage);
                        RealTimeDataChild2Fragment.setSensorMessage(sensorMessageBean.getRows(), sensorMessage);
                        RealTimeDataChild3Fragment.setSensorMessage(sensorMessageBean.getRows(), sensorMessage);
                        RealTimeDataChild4Fragment.setSensorMessage(sensorMessageBean.getRows(), sensorMessage);
                    }

                    @Override
                    protected void _onError(ApiException ex) {
                        ToastMessage(ex.message);
                        dismissLoading();
                    }
                });
    }
}
